package com.xjc.business.controller.schadmin;

import com.xjc.business.entity.po.MsgPo;
import com.xjc.business.entity.vo.MsgVo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.service.SysMsgService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 徐锦程
 * 时间:2025-01-26 10:08
 */
@RestController
@RequestMapping("/sch_admin/msg")
public class MsgController {

    @Autowired
    SysMsgService sysMsgService;

    /**
     * 获取消息分页
     * @param pageSize  每页条数
     * @param currentPage 当前页码
     * @param schoolId 接收方id
     * @param readStatus 阅读状态
     * @param type 类型
     * @return 分页数据
     */
    @GetMapping("/receivePage/{pageSize}/{currentPage}")
    public Result<PageVo<MsgVo>> getMsgReceivePage(
            @PathVariable(value = "pageSize",required = false) Integer pageSize,
            @PathVariable(value = "currentPage",required = false) Integer currentPage,
            Integer schoolId,
            Integer readStatus,
            Integer type
            ){
        if(schoolId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }
        if(pageSize == null || pageSize <= 0){
            pageSize = 10;
        }
        if(currentPage == null || currentPage <= 0){
            currentPage = 1;
        }
        if(readStatus == null){
            readStatus = -1;
        }
        if(type == null){
            type = -1;
        }

        return sysMsgService.getMsgPageWithUserId(schoolId,pageSize,currentPage,readStatus,type);
    }

    /**
     * 获取消息分页
     * @param pageSize  每页条数
     * @param currentPage 当前页码
     * @param readStatus 阅读状态
     * @param type 类型
     * @return 分页数据
     */
    @GetMapping("/page/{pageSize}/{currentPage}")
    public Result<PageVo<MsgVo>> getMsgPage(
            @PathVariable(value = "pageSize",required = false) Integer pageSize,
            @PathVariable(value = "currentPage",required = false) Integer currentPage,
            Integer sendId,
            Integer readStatus,
            Integer type
    ){
        if(sendId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }
        if(pageSize == null || pageSize <= 0){
            pageSize = 10;
        }
        if(currentPage == null || currentPage <= 0){
            currentPage = 1;
        }
        if(readStatus == null){
            readStatus = -1;
        }
        if(type == null){
            type = -1;
        }

        return sysMsgService.getMsgPage(sendId,pageSize,currentPage,readStatus,type);
    }


    /**
     * 新增消息
     * @param msgPo 消息
     * @return 反馈
     */
    @PostMapping("/add")
    public Result<String> addMsg(@RequestBody(required = false) MsgPo msgPo){
        if(msgPo == null || msgPo.getReviceId() == null || msgPo.getSendId() == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return sysMsgService.addMsg(msgPo);
    }

    /**
     * 消息已读
     * @param id 消息id
     * @return 反馈
     */
    @PostMapping("/read/{id}")
    public Result<String> readMsg(@PathVariable(value = "id",required = false) Integer id){
        if(id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return sysMsgService.readMsg(id);
    }
}
